

//
//  CommitOrderViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/8/3.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//
#import "CommitOrderViewController.h"
#import "OrderInforCell.h"
@interface CommitOrderViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSInteger totalNumber;
    float priceValue;
}
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UIImageView *centerImageView;
@property(nonatomic,strong)UILabel *numLabel;
@property(nonatomic,strong)UILabel *totalLabel;
@property(nonatomic,strong)UILabel *priceLabel;
@property (strong, nonatomic) NSMutableArray<CALayer *> *redLayers;

@property (strong, nonatomic) NSArray *priceArr;
@end

@implementation CommitOrderViewController
- (NSMutableArray<CALayer *> *)redLayers {
    if (!_redLayers) {
        _redLayers = [NSMutableArray array];
    }
    return _redLayers;
}
-(NSArray *)priceArr{
    if (_priceArr == nil) {
        _priceArr = @[@"15.8",@"30.4",@"80",@"100",@"200"];
    }
    return _priceArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单信息";
    [self createTopview];
    [self createUIOfTableview];
    // Do any additional setup after loading the view.
}
//创建头部视图
-(void)createTopview{
    UILabel *consigneeLabel = [[UILabel alloc] init];
    [self.view addSubview:consigneeLabel];
    [consigneeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(94);
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(40));
        make.height.mas_equalTo(scaleTopAndBottom(20));
        
    }];
    UILabel *phoneLabel = [[UILabel alloc] init];
    [self.view addSubview:phoneLabel];
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(94);
        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-40));
        make.height.mas_equalTo(scaleTopAndBottom(20));
        make.left.equalTo(consigneeLabel.mas_right);
        make.width.equalTo(consigneeLabel.mas_width);
    }];
    UIImageView *localImageView = [[UIImageView alloc] init];
    [self.view addSubview:localImageView];
    [localImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(10));
        make.size.mas_equalTo(CGSizeMake(25, 25));
        make.top.equalTo(consigneeLabel.mas_bottom);
    }];
    UILabel *addressLabel = [[UILabel alloc] init];
    [self.view addSubview:addressLabel];
    [addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(consigneeLabel.mas_bottom).offset(5);
        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-40));
        make.height.mas_equalTo(scaleTopAndBottom(20));
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(40));
    }];
    UIImageView *arrorwImageView = [[UIImageView alloc] init];
    [self.view addSubview:arrorwImageView];
    [arrorwImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-10));
        make.size.mas_equalTo(CGSizeMake(15, 15));
        make.top.equalTo(phoneLabel.mas_bottom).offset(scaleTopAndBottom(5));
    }];
    UIImageView *lineImageView = [[UIImageView alloc] init];
    [self.view addSubview:lineImageView];
    [lineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        make.top.equalTo(addressLabel.mas_bottom).offset(scaleTopAndBottom(20));
        make.height.mas_equalTo(scaleTopAndBottom(15));
    }];
    self.centerImageView = lineImageView;
    arrorwImageView.backgroundColor = [UIColor redColor];
    consigneeLabel.backgroundColor = [UIColor cyanColor];
    phoneLabel.backgroundColor = [UIColor greenColor];
    localImageView.backgroundColor = [UIColor orangeColor];
    addressLabel.backgroundColor = [UIColor blackColor];
    lineImageView.backgroundColor = [UIColor cyanColor];
}
-(void)createUIOfTableview{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.centerImageView.mas_bottom);
        make.left.and.right.equalTo(self.view);
//        make.height.mas_equalTo(scaleTopAndBottom(335));
        make.bottom.equalTo(self.view.mas_bottom).offset(scaleTopAndBottom(-50));
    }];
    [self.tableView registerClass:[OrderInforCell class] forCellReuseIdentifier:@"OrderInforCell"];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor yellowColor];
    _totalLabel = [[UILabel alloc] init];
    [self.view addSubview:_totalLabel];
    [_totalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.bottom.equalTo(self.view);
        make.top.equalTo(self.tableView.mas_bottom);
        make.width.mas_equalTo(WIDTH * 0.7);
    }];
    UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:commitBtn];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.and.bottom.equalTo(self.view);
        make.top.equalTo(self.tableView.mas_bottom);
        make.left.equalTo(_totalLabel.mas_right);
    }];
//    _totalLabel.backgroundColor = [UIColor redColor];
    commitBtn.backgroundColor = [UIColor yellowColor];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderInforCell *inforCell = [tableView dequeueReusableCellWithIdentifier:@"OrderInforCell"];
    NSString *str = [NSString stringWithFormat:@"%@ %@",@"￥",self.self.priceArr[indexPath.row]];
    inforCell.priceLabel.text = str;
    [inforCell setPlusTapHandle:^(CGPoint position) {
        totalNumber++;
        priceValue += [self.priceArr[indexPath.row] floatValue];
        [self updateNumberLabel];
    }];
    
    [inforCell setMinusTapHandle:^() {
        totalNumber--;
        priceValue -= [self.priceArr[indexPath.row] floatValue];
        [self updateNumberLabel];
    }];
    return inforCell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return scaleTopAndBottom(100.f);
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    _priceLabel = [[UILabel alloc] init];
    [view addSubview:_priceLabel];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view.mas_centerY);
        make.right.equalTo(view.mas_right);
        make.width.mas_equalTo(scaleLeftAndRight(120));
        make.height.mas_equalTo(scaleTopAndBottom(25.f));
    }];
    self.numLabel = [[UILabel alloc] init];
    [view addSubview:_numLabel];
    [_numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view.mas_centerY);
        make.right.equalTo(_priceLabel.mas_left).offset(scaleLeftAndRight(-20));
        make.left.equalTo(view.mas_left);
        make.height.mas_equalTo(scaleTopAndBottom(25.f));
    }];
    _priceLabel.backgroundColor = [UIColor orangeColor];
    self.numLabel.backgroundColor = [UIColor redColor];
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return scaleTopAndBottom(50.f);
}
- (void)initCHLayerFromPoint:(CGPoint)startPoint toPoint:(CGPoint)endPoint {
    
    CALayer *chLayer = [[CALayer alloc] init];
    [self.redLayers addObject:chLayer];
    
    chLayer.frame = CGRectMake(startPoint.x, startPoint.y, 15, 15);
    chLayer.cornerRadius = CGRectGetWidth(chLayer.frame)/2.f;
    chLayer.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:chLayer];
    
    CAKeyframeAnimation *CHAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, startPoint.x, startPoint.y);
    CGPathAddQuadCurveToPoint(path, NULL, endPoint.x, startPoint.y, endPoint.x, endPoint.y);
    
    CHAnimation.path = path;
    CHAnimation.removedOnCompletion = NO;
    CHAnimation.fillMode = kCAFillModeBoth;
    CHAnimation.duration = 0.5;
    CHAnimation.delegate = self;
    
    [chLayer addAnimation:CHAnimation forKey:nil];
    
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    [self.redLayers[0] removeFromSuperlayer];
    [self.redLayers removeObjectAtIndex:0];
    
}

- (void)updateNumberLabel{
    
    NSString *totalNumberStr = [NSString stringWithFormat:@"共 %zd 件商品",totalNumber];
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalNumberStr];
    [attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16.f] range:NSMakeRange(1, attributedStr.length - 4)];
    self.numLabel.attributedText = attributedStr;
    NSMutableAttributedString *totalLabelStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%.2f",@"合计:￥",priceValue]];
    [totalLabelStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,2)];
    [totalLabelStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3, totalLabelStr.length-3)];
    self.totalLabel.attributedText = totalLabelStr;
    NSMutableAttributedString *priceLabelStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%.2f",@"小计:￥",priceValue]];
    [priceLabelStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, 2)];
    [priceLabelStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3, priceLabelStr.length-3)];
    self.priceLabel.attributedText = priceLabelStr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
